# -*- coding: UTF-8 -*-

import xlsxwriter


class HtmlOutputer(object):

    def __init__(self):
        self.datas = []

    def collect_data(self, data):
        if data is None:
            return
        self.datas.append(data)

    def save_data(self):

        row0 = [u'书名', u'价格',u'原价',u"累计评价"]

        workbook = xlsxwriter.Workbook('./file/data.xlsx')

        bold_format = workbook.add_format({'bold': True,'align':'center','font_size':14})
        money_format = workbook.add_format({'num_format': '￥#,###.00'})

        worksheet = workbook.add_worksheet()
        worksheet.set_column(0, 0, 100)
        worksheet.set_column(1, 1, 10)
        worksheet.set_column(2, 2, 10)
        worksheet.set_column(3, 3, 20)
        worksheet.write(0, 0, row0[0], bold_format)
        worksheet.write(0, 1, row0[1], bold_format)
        worksheet.write(0, 2, row0[2], bold_format)
        worksheet.write(0, 3, row0[3], bold_format)
        i = 1
        for data in self.datas:
            worksheet.write(i, 0, data['name'])
            worksheet.write(i, 1, data['price'], money_format)
            worksheet.write(i, 2, data['ori_price'], money_format)
            worksheet.write(i, 3, data['evaluate'], money_format)
            i = i + 1
        workbook.close()
